package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.BusinessException;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.ClassDao;
import com.fuli.app.dto.StudyClassDTO;
import com.fuli.app.dto.query.StudyClassQueryDTO;
import com.fuli.app.entity.StudyClass;
import com.fuli.app.service.ClassService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassDao classDao;

    @Override
    public ActionResult<String> add(StudyClassDTO dto) {

        StudyClass po = EntityUtils.copyObjectWithInit(dto, StudyClass.class);
        po.setId(EntityUtils.uuid());
        classDao.insert(po);
        return new ActionResult<>(po.getId());
    }

    @Override
    public ActionResult<String> delete(String id) {
        if (classDao.get(id) == null)
            return new ActionResult<>(id);
        classDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        classDao.batchDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<StudyClassDTO> get(String id) {
        StudyClass aStudyClass = classDao.get(id);
        StudyClassDTO StudyClassDTO = EntityUtils.copyObject(aStudyClass, StudyClassDTO.class);
        return new ActionResult<>(StudyClassDTO);
    }

    @Override
    public ActionResult<Pagination<StudyClassDTO>> listByQueryDTO(StudyClassQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = classDao.getCountByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<StudyClass> studyClasses = classDao.listByQueryDTO(queryDTO);
            List<StudyClassDTO> dtos = EntityUtils.copyList(studyClasses, StudyClassDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }

    @Override
    public ActionResult<String> update(StudyClassDTO dto) {
        StudyClass aStudyClass = classDao.get(dto.getId());
        if (aStudyClass == null)
            throw new BusinessException(-1, "修改失败，id不存在");
//            return new ActionResult<>(-1, "修改失败，id不存在");
        aStudyClass.setAmount(dto.getAmount());
        aStudyClass.setName(dto.getName());
        EntityUtils.update(aStudyClass);
        classDao.update(aStudyClass);
        return new ActionResult<>(dto.getId());
    }

    public ActionResult<String> addCurrentHeadcount(String id) {
        StudyClass aStudyClass = classDao.get(id);
        if (aStudyClass == null)
            throw new BusinessException(-1, "添加失败，数据不存在");
        aStudyClass.setCurrentHeadcount(aStudyClass.getCurrentHeadcount() + 1);
        classDao.addCurrentHeadcount(aStudyClass);
        return new ActionResult<>(id);
    }

}
